AC_INIT([opencalphad], [master], [clement.introini@cea.fr], [opencalphad])
### minimum version of autoconf :
AC_PREREQ(2.50)
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([config])


dnl Initialize automake.  automake < 1.12 didn't have serial-tests and
dnl gives an error if it sees this, but for automake >= 1.13
dnl serial-tests is required so we have to include it.  Solution is to
dnl test for the version of automake (by running an external command)
dnl and provide it if necessary.  Note we have to do this entirely using
dnl m4 macros since automake queries this macro by running
dnl 'autoconf --trace ...'.
m4_define([serial_tests], [
  m4_esyscmd([automake --version |
              head -1 |
              awk '{split ($NF,a,"."); if (a[1] == 1 && a[2] >= 12) { print "serial-tests" }}'])])

# Automake specific stuff
AM_INIT_AUTOMAKE(
	foreign 
	dist-bzip2 
	tar-ustar 
	serial_tests)

### initializes canonical host variables: host, host_cpu, host_vendor and host_os
AC_CANONICAL_HOST
AC_PROG_MAKE_SET
AC_PROG_INSTALL




# Checks for programs.
# store current user given compiler flags to avoid default setup via AC_PROG_FC
#OLD_FFLAGS=$FFLAGS
#OLD_FCFLAGS=$FCFLAGS
# store current user given compiler flags to avoid default setup via AC_PROG_CXX
OLD_CXXFLAGS=$CXXFLAGS
AC_PROG_CXXCPP
AC_PROG_CXX
AC_PROG_FC([gfortran])
AC_LANG(Fortran)
AC_PROG_LIBTOOL

# reset compiler flags to initial flags
CXXFLAGS=$OLD_CXXFLAGS
#FFLAGS=$OLD_FFLAGS
#FCFLAGS=$OLD_FCFLAGS

dnl enable silent rules
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

dnl--------------------------------------------------------------------
dnl enable-debug for compilling with debug options (no by default)
dnl--------------------------------------------------------------------
AC_ARG_ENABLE([debug],
[  --enable-debug    Turn on debugging],
[case "${enableval}" in
  yes) debug=true ;;
  no)  debug=false ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
esac],[debug=false])

if test "x$debug" = "xtrue"
then
FCFLAGS=" -g -ggdb -fcheck=all -ffpe-trap=invalid,zero,overflow "
fi
dnl--------------------------------------------------------------------
dnl enable-openmp for compilling with openmp options (no by default)
dnl--------------------------------------------------------------------
AC_ARG_ENABLE([openmp],
[  --enable-openmp    Turn on debugging],
[case "${enableval}" in
  yes) openmp=true ;;
  no)  openmp=false ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --enable-openmp]) ;;
esac],[openmp=false])
AM_CONDITIONAL([OPENMP], [test x$openmp = xtrue])

if test "x$openmp" = "xtrue" 
then
AC_OPENMP
OPENMPLIB="$OPENMP_FCFLAGS"
AC_SUBST(OPENMPLIB)
if test "x$OPENMPLIB" != "x"
then
OPENMP_FFLAGS="$OPENMP_FCFLAGS"
fi
AC_SUBST(OPENMP_FFLAGS)
AC_SUBST(OPENMP_FCFLAGS)
fi



dnl--------------------------------------------------------------------
dnl with-python for compiling python wrapper
dnl--------------------------------------------------------------------
pyOC=no
AC_ARG_WITH(python,
    AC_HELP_STRING([--with-python],[compile python wrapper]),
    [],[withval="no"])
if test "x$withval" == "xyes" ; then
  pyOC=yes
  AM_PATH_PYTHON([3.6])
fi
AM_CONDITIONAL(WITH_PYTHON, test "x$pyOC" = "xyes")

dnl--------------------------------------------------------------------
dnl with-lapack for compiling with user lapack library
dnl--------------------------------------------------------------------
lapack=no
AC_ARG_WITH(lapack,
    AC_HELP_STRING([--with-lapack],[compile with user lapack library)]),
    [],[withval="no"])
if test "x$withval" == "xyes" ; then
  lapack=yes
fi
AM_CONDITIONAL(WITH_LAPACK, test "x$lapack" = "xyes")


dnl--------------------------------------------------------------------
dnl with-ochelp for compiling with browser help on Linux, command line
dnl editing and open files
dnl--------------------------------------------------------------------
ocHelp=no
AC_ARG_WITH(ochelp,
    AC_HELP_STRING([--with-ochelp],[compile with browser help on Linux]),
    [],[withval="no"])
if test "x$withval" == "xyes" ; then
  ocHelp=yes
fi
AM_CONDITIONAL(WITH_OCHELP, test "x$ocHelp" = "xyes")


dnl--------------------------------------------------------------------
dnl with-xplot for compiling with Gnuplot 
dnl--------------------------------------------------------------------
xplot=no
AC_ARG_WITH(xplot,
    AC_HELP_STRING([--with-xplot],[compile with Gnuplot]),
    [],[withval="no"])
if test "x$withval" == "xyes" ; then
  xplot=yes
fi
AM_CONDITIONAL(WITH_OCPLOT, test "x$xplot" = "xyes")


#####################################################################
# Generate configuration file envOC.sh to set environment for running
# tests directly from source and build directory
#####################################################################
echo '# This script is generated by configure' > envOC.sh
echo '# Use it to set environment for running from build dir' >> envOC.sh
BUILDDIR=`pwd`
echo "export BUILDDIR=$BUILDDIR" >> envOC.sh
SRCDIR=`dirname $0`
cd $SRCDIR && SRCDIR=`pwd` && cd $BUILDDIR
echo "export SRCDIR=$SRCDIR" >> envOC.sh
echo "export OPENCALPHAD_ROOT=$SRCDIR" >> envOC.sh
echo "export OCHOME=$SRCDIR/doc/manual/" >> envOC.sh
echo ""  >> envOC.sh

echo 'LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BUILDDIR/src/.libs:$BUILDDIR/src/utilities/.libs:$BUILDDIR/src/numlib/.libs:$BUILDDIR/src/models/.libs:$BUILDDIR/.libs:$BUILDDIR/src/minimizer/.libs' >> envOC.sh

echo 'PATH=$BUILDDIR:$PATH' >> envOC.sh
echo "export PATH" >> envOC.sh
echo "" >> envOC.sh
###################

AC_CONFIG_FILES([
				Makefile])

